package com.pda.common.pages.createinbill;

import android.content.Context;
import com.apkfuns.logutils.LogUtils;
import com.pda.common.basemvp.model.BaseModel;
import com.pda.common.bean.BillBean;
import com.pda.common.beanin.BillInBean;
import com.pda.common.database.BillInfo;
import com.pda.common.database.DataBaseControl;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.tools.StringProcess;
import com.pda.common.utils.PDAUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class CreateInBillModelImpl extends BaseModel implements CreateInBillModel {
        private String TAG = "CreateInBillModelImpl";
    BillBean billBean;

    public CreateInBillModelImpl(Context context) {
        super(context);
    }

    @Override
    public void createBill(String batch, String pid, final CallBack callBack) {
        try {
            BillInBean billInBean = new BillInBean();
            billInBean.setAction("AddBill_Detail");
            billInBean.setCid(SharedPreferencesManger.getCid());
            billInBean.setUserID(SharedPreferencesManger.getUID());
            billInBean.setStoreID(SharedPreferencesManger.getStoreID());
            billInBean.setBillType(PDAUtil.IN);
            billInBean.setProductID(pid);
            billInBean.setBatch(batch);
            billInBean.setBoxNumsAll(0);
            RequestBody body = RequestBody.create(PDAUtil.JSON, gson.toJson(billInBean));
            httpConfig.createBill(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.onCreateBillSucc(billBean);
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (billBean.getErrmsg() != null && billBean.getErrmsg().length() > 0) {
                                callBack.ontip(billBean.getErrmsg());
                            } else {
                                callBack.ontip(PDAUtil.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            billBean = gson.fromJson(StringProcess.responsetoString(abBean), BillBean.class);
                            if (billBean != null && billBean.getErrcode() == 0) {
                                BillInfo billInfo = new BillInfo();
                                billInfo.setBillCode(billBean.getData().get(0).getBillCode());
                                billInfo.setDesname(billBean.getData().get(0).getDesStoreName());
                                billInfo.setType(PDAUtil.IN);
                                DataBaseControl.addBill(billInfo);
                                onComplete();
                            }
                            else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(PDAUtil.connectError);
            e.printStackTrace();
        }
    }
}

